<script lang="ts" setup>
import { getOrderPage, updateOrder, getOrderForm, addOrder } from "@/api/order";
import { OrderPageVO, OrderForm, OrderQuery } from "@/api/order/types";

defineOptions({
  name: "Orders",
  inheritAttrs: false,
});
const queryFormRef = ref(ElForm);
const orderFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<OrderQuery>({
  pageNum: 1,
  pageSize: 10,
});

const orderList = ref<OrderPageVO[]>();
const dialog = reactive({
  title: "",
  visible: false,
});

const formData = reactive<OrderForm>({});
const rules = reactive({
  name: [{ required: true, message: "请输入订单名称", trigger: "blur" }],
  mId: [{ required: true, message: "请选择会员", trigger: "blur" }],
  amount: [{ required: true, message: "请输入金额", trigger: "blur" }],
});
function handleSizeChange(val) {
  console.log(`每页 ${val} 条`);
}
function handleCurrentChange(val) {
  console.log(`当前页: ${val}`);
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  getOrderPage(queryParams)
    .then(({ data }) => {
      orderList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行checkbox 选中事件 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/** 打开订单表单弹窗 */
function openDialog(orderId?: number) {
  dialog.visible = true;
  if (orderId) {
    dialog.title = "修改订单";
    getOrderForm(orderId).then(({ data }) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增订单";
  }
}

/** 订单保存提交 */
function handleSubmit() {
  orderFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const orderId = formData.id;
      if (orderId) {
        updateOrder(orderId, formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addOrder(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  orderFormRef.value.resetFields();
  orderFormRef.value.clearValidate();

  formData.id = undefined;
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :inline="true" :model="queryParams">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            clearable
            placeholder="订单名称、编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="statusFlag">
          <el-select
            v-model="queryParams.statusFlag"
            class="!w-[100px]"
            clearable
            placeholder="全部"
          >
            <el-option label="待上传" value="0" />
            <el-option
              label="待审核"
              value="1"
            /><!--订单状态(0-待上传；1-待审核；2-待放款；3-待结算；4-申请撤销；5-订单成功；6-订单关闭)-->
            <el-option label="待放款" value="2" />
            <el-option label="待结算" value="3" />
            <el-option label="申请撤销" value="4" />
            <el-option label="订单成功" value="5" />
            <el-option label="订单关闭" value="6" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card class="table-container m-10px b-0" shadow="never">
      <template #header>
        <el-button type="success" @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="orderList"
        border
        highlight-current-row
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column label="订单编号" prop="code" width="150" />
        <el-table-column label="订单名称" prop="name" width="200" />
        <el-table-column label="金额（元）" prop="amount" width="150" />
        <el-table-column align="center" label="会员" prop="mName" width="100" />
        <el-table-column
          align="center"
          label="状态"
          prop="status"
          width="100"
        />
        <el-table-column
          align="center"
          label="业务员"
          prop="uName"
          width="80"
        />
        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <div v-if="scope.row.statusFlag == 0">
              <el-button
                size="small"
                type="primary"
                @click="openDialog(scope.row.id)"
                >上传</el-button
              >
              <el-button
                size="small"
                type="success"
                @click="openDialog(scope.row.id)"
                >修改</el-button
              >
            </div>
            <div v-else-if="scope.row.statusFlag == 1">
              <el-button
                size="small"
                type="danger"
                @click="openDialog(scope.row.id)"
                >审核</el-button
              >
              <el-button
                size="small"
                type="warning"
                @click="openDialog(scope.row.id)"
                >申请撤销</el-button
              >
            </div>
            <div v-else-if="scope.row.statusFlag == 2">
              <el-button
                size="small"
                type="success"
                @click="openDialog(scope.row.id)"
                >放款</el-button
              >
              <el-button
                size="small"
                type="warning"
                @click="openDialog(scope.row.id)"
                >申请撤销</el-button
              >
            </div>
            <div v-else-if="scope.row.statusFlag == 3">
              <el-button
                size="small"
                type="danger"
                @click="openDialog(scope.row.id)"
                >结算</el-button
              >
              <el-button
                size="small"
                type="warning"
                @click="openDialog(scope.row.id)"
                >申请撤销</el-button
              >
            </div>
            <div v-else-if="scope.row.statusFlag == 4">
              <el-button
                size="small"
                type="danger"
                @click="openDialog(scope.row.id)"
                >同意撤销</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        v-model:total="total"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 订单表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="closeDialog"
    >
      <el-form
        ref="orderFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="订单名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入订单名称" />
        </el-form-item>
        <el-form-item label="订单金额" prop="name">
          <el-input v-model="formData.amount" placeholder="请输入订单金额" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
